﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;
namespace cebao.Web.plantinfo
{
    /// <summary>
    /// GetPlant 的摘要说明
    /// </summary>
    public class GetPlant : IHttpHandler, IRequiresSessionState
    {
        StringBuilder result = new StringBuilder();
        StringBuilder rechild = new StringBuilder();
        StringBuilder sb = new StringBuilder();
        cebao.BLL.plantinfo bll = new BLL.plantinfo();
        public void ProcessRequest(HttpContext context)
        {
            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }
            if (ac == "querypro")
                Querypro(context);
            else  if (ac == "query")
                Query(context);
            else if (ac == "add")
                add(context);
            else if (ac == "modify")
                modify(context);
            else if (ac == "delete")
                delete(context);
            else if (ac == "queryc")
                Querychild(context);
            else if (ac == "queryr")
                QueryRoot(context);
            else if (ac == "querymy")
                Querymy(context);
            else if (ac == "querymyuid")
                Querymyuid(context);
            else if (ac == "queryus")
                Queryus(context);
            else if (ac == "addmynode")
                addmy(context);
            else if (ac == "addmyuidnode")
                addmyuid(context);
            else if (ac == "deletemy")
                deletemy(context);
            else if (ac == "deletemyuid")
                deletemyuid(context);

        }
        public void deletemyuid(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int uid = 0;
                string uidstr = context.Request.QueryString["upid"];
                if (uidstr != "")
                    uid = int.Parse(uidstr);
                cebao.BLL.userplant mybll = new BLL.userplant();
                if (mybll.Delete(uid))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();

                }
            }

        }
        public void deletemy(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int uid = 0;
                string uidstr = context.Request.QueryString["mpid"];
                if (uidstr != "")
                    uid = int.Parse(uidstr);
                cebao.BLL.myplant mybll = new BLL.myplant();
                if (mybll.Delete(uid))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();

                }
            }

        }
        public void addmy(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int hid = 0;
            string hids = context.Request.QueryString["pid"];
            if (hids != "") hid = int.Parse(hids);
            if (hid != 0)
            {
                string province = Web.Admin.myinfo.getPCC(context).Split(',')[0];
                int uid = Web.Admin.myinfo.getID(context);
                if (province != "")
                {
                    cebao.Model.myplant model = new cebao.Model.myplant();
                    model.pid = hid;
                    model.uid = uid;
                    model.province = province;
                    cebao.BLL.myplant mybll = new BLL.myplant();
                    if (mybll.Add(model) > 0)
                    {
                        context.Response.Write("T");//返回给前台页面  
                        context.Response.End();

                    }
                    else
                    {
                        context.Response.Write("F");//返回给前台页面  
                        context.Response.End();
                    }
                }
                else
                {
                    context.Response.Write("L");//返回给前台页面  
                    context.Response.End();
                }
            }

        }
        public void addmyuid(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int hid = 0;
            string hids = context.Request.QueryString["pid"];
            if (hids != "") hid = int.Parse(hids);
            if (hid != 0)
            {
                string uids = context.Request.QueryString["uid"];
                int uid = 0;
                int.TryParse(uids, out uid);
                if (uid != 0)
                {
                    cebao.Model.userplant model = new cebao.Model.userplant();
                    model.mpid = hid;
                    model.uid = uid;
                    cebao.BLL.userplant mybll = new BLL.userplant();
                    if (mybll.Add(model) > 0)
                    {
                        context.Response.Write("T");//返回给前台页面  
                        context.Response.End();

                    }
                    else
                    {
                        context.Response.Write("F");//返回给前台页面  
                        context.Response.End();
                    }
                }
                else
                {
                    context.Response.Write("L");//返回给前台页面  
                    context.Response.End();
                }
            }

        }
        private string GetTreeJsonByTableOne(DataTable tabel, string idCol, string txtCol, string bmCol)
        {
            result.Remove(0, result.Length);
            if (tabel.Rows.Count > 0)
            {
                result.Append("[");

                foreach (DataRow row in tabel.Rows)
                {
                    result.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol] + "\",\"bm\":\"" + row[bmCol] + "\"},");
                }
                result = result.Remove(result.Length - 1, 1);

                result.Append("]");

            }
            return result.ToString();
        }
        public void Querymy(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                cebao.BLL.myplant mybll = new BLL.myplant();
                string pro = Web.Admin.myinfo.getPCC(context).Split(',')[0];
                DataSet ds = mybll.GetListv(" province='" + pro + "'");
                string str = GetTreeJsonByTableOne(ds.Tables[0], "mpid", "pname", "pbm");
                context.Response.Write(str);//返回给前台页面  
                context.Response.End();
            }
        }
        public void Querymyuid(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                string uids = context.Request.Params["uid"];
                int uid = 0;
                int.TryParse(uids, out uid);
                cebao.BLL.myplant mybll = new BLL.myplant();
                DataSet ds = mybll.GetListvuid(" uid=" + uid );
                string str = GetTreeJsonByTableOne(ds.Tables[0], "upid", "pname", "pbm");
                context.Response.Write(str);//返回给前台页面  
                context.Response.End();
            }
        }
        public void Queryus(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                BLL.userinfo ubll = new BLL.userinfo();
                string uids = context.Request.Params["uid"];
                int uid = 0;
                int.TryParse(uids, out uid);
                cebao.BLL.myplant mybll = new BLL.myplant();
                string pro = ubll.Getpro(uid);
                DataSet ds = mybll.GetListv(" province='" + pro + "'");
                string str = GetTreeJsonByTableOne(ds.Tables[0], "mpid", "pname", "pbm");
                context.Response.Write(str);//返回给前台页面  
                context.Response.End();
            }
        }
        public void Querypro(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                string pro = Web.Admin.myinfo.getPCC(context).Split(',')[0];

                StringBuilder strWhere = new StringBuilder();
                if (pro != "")
                {
                    strWhere.AppendFormat(" province='{0}'  ", pro);
                }
                cebao.BLL.myplant mybll = new BLL.myplant();                
                DataSet ds = mybll.GetListv(strWhere.ToString());
                string str = GetTreeJsonByTableOne(ds.Tables[0], "mpid", "pname", "pbm");
                context.Response.Write(str);//返回给前台页面  
                context.Response.End();
            }


        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = 0;
            string uidstr = context.Request.QueryString["pid"];
            if (uidstr != "")
                uid = int.Parse(uidstr);           
            if (bll.Delete(uid))
            {
                context.Response.Write("T");//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();

            }

        }
        public void modify(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                string pname = context.Request.Form["pname"];
                int pid = int.Parse(context.Request.Form["pid"]);
                string pbm = context.Request.Form["pbm"];

                cebao.Model.plantinfo model = new cebao.Model.plantinfo();
                model.pname = pname;
                model.pid = pid;
                model.pbm = pbm;
                if (bll.Update(model))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();

                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
        }
        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string pname = context.Request.Form["pname"];
            string pbm = context.Request.Form["pbm"];
           
            if (Web.Admin.myinfo.isLogin(context))
            {
                int parentid = 0;
                if (context.Request.Form["parentid"] != "")
                    parentid = int.Parse(context.Request.Form["parentid"]);
                cebao.Model.plantinfo model = new cebao.Model.plantinfo();
                model.pname = pname;
                model.parentid = parentid;
                model.pbm = pbm;
                //model.province = province;               
                if (bll.Add(model) > 0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();

                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }

        private string GetTreeJsonByTable(DataTable tabel, string idCol, string txtCol, string url, string rela, object pId)
        {
            result.Append(sb.ToString());
            sb.Remove(0, sb.Length);
            if (tabel.Rows.Count > 0)
            {
                sb.Append("[");
                string filer = string.Format("{0}='{1}'", rela, pId);
                DataRow[] rows = tabel.Select(filer);
                if (rows.Length > 0)
                {
                    foreach (DataRow row in rows)
                    {
                        sb.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol] + "\",\"bm\":\"" + row["pbm"] +"\"");
                        if (tabel.Select(string.Format("{0}='{1}'", rela, row[idCol])).Length > 0)
                        {
                            //点击展开
                            sb.Append(",\"children\":");
                            GetTreeJsonByTable(tabel, idCol, txtCol, url, rela, row[idCol]);
                            result.Append(sb.ToString());
                            sb.Remove(0, sb.Length);
                        }
                        result.Append(sb.ToString());
                        sb.Remove(0, sb.Length);
                        sb.Append("},");
                    }
                    sb = sb.Remove(sb.Length - 1, 1);
                }
                sb.Append("]");
                result.Append(sb.ToString());
                sb.Remove(0, sb.Length);
            }
            return result.ToString();
        }
        private string GetTreechild(DataTable tabel, string idCol, string rela, object pId)
        {
            rechild.Append(sb.ToString());
            sb.Remove(0, sb.Length);
            if (tabel.Rows.Count > 0)
            {                
                string filer = string.Format("{0}='{1}'", rela, pId);
                DataRow[] rows = tabel.Select(filer);
                if (rows.Length > 0)
                {
                    foreach (DataRow row in rows)
                    {
                        sb.Append( row[idCol] + ",");
                        if (tabel.Select(string.Format("{0}='{1}'", rela, row[idCol])).Length > 0)
                        {                            
                            GetTreechild(tabel, idCol, rela, row[idCol]);
                            rechild.Append(sb.ToString());
                            sb.Remove(0, sb.Length);
                        }
                        rechild.Append(sb.ToString());
                        sb.Remove(0, sb.Length);                        
                    }
                    //sb = sb.Remove(sb.Length - 1, 1);
                }
                rechild.Append(sb.ToString());
                sb.Remove(0, sb.Length);
            }
            return rechild.ToString();
        }
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                DataSet ds = bll.GetList("");
                string str = GetTreeJsonByTable(ds.Tables[0], "pid", "pname", "", "parentid", 0);
                context.Response.Write(str);//返回给前台页面  
                context.Response.End();
            }

        }
        public void Querycombo(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string pro = Web.Admin.myinfo.getPCC(context).Split(',')[0];

            StringBuilder strWhere = new StringBuilder();
            if (pro != "")
            {
                strWhere.AppendFormat(" province='{0}'  ", pro);
            }
            cebao.BLL.myplant mybll = new BLL.myplant();
            DataSet ds = mybll.GetListv(strWhere.ToString());
            string str = GetTreeJsonByTable(ds.Tables[0], "pid", "pname", "", "parentid", 0);
            context.Response.Write(str);//返回给前台页面  
            context.Response.End();

        }
        public void QueryRoot(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string pro = Web.Admin.myinfo.getPCC(context).Split(',')[0];

            StringBuilder strWhere = new StringBuilder();
            if (pro != "")
            {
                strWhere.AppendFormat(" province='{0}'  ", pro);
            }
            cebao.BLL.myplant mybll = new BLL.myplant();
            DataSet ds = mybll.GetListv(strWhere.ToString());

            string str = GetTreeJsonByTableOne(ds.Tables[0], "mpid", "pname", "pbm");
            str = "[{\"id\":0,\"text\":\"全部\",\"children\":" + str + "}]";
            context.Response.Write(str);//返回给前台页面  
            context.Response.End();

        }
        public void Querychild(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string pro = Web.Admin.myinfo.getPCC(context).Split(',')[0];
            string pid = context.Request.QueryString["pid"];
            StringBuilder strWhere = new StringBuilder();
            if (pro != "")
            {
                strWhere.AppendFormat(" province='{0}'  ", pro);
            }
            DataSet ds = bll.GetList(strWhere.ToString());

            string str = GetTreechild(ds.Tables[0], "pid", "parentid", pid);
            if (str == "")
                str = pid;
            else
                str += pid;
            string json = "[{\"ids\":\"" + str+"\"}]";
            context.Response.Write(json);//返回给前台页面  
            context.Response.End();

        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}